package ah.byte2.extend;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;

import org.apache.mina.core.buffer.IoBuffer;
import org.junit.Test;

public class ExtendArray {

	@Test
	public void mina() {
		IoBuffer buffer = IoBuffer.allocate(16);
		
		//buffer.setAutoExpand(true);
		
		buffer.setAutoShrink(true);
		buffer.put((byte)1);
		System.out.println("Initial Buffer capacity = "+buffer.capacity());
		buffer.shrink();
		System.out.println("Initial Buffer capacity after shrink = "+buffer.capacity());

		buffer.capacity(32);
		System.out.println("Buffer capacity after incrementing capacity to 32 = "+buffer.capacity());
		buffer.shrink();
		System.out.println("Buffer capacity after shrink= "+buffer.capacity());
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			URL url = new URL("http://www.xapcn.com/uploads/allimg/110604/1-110604001P1.jpg");//new URL(scard.getPicPath());
			
			/*
			 * 扩展数组长度,复制数组
			 * System.arraycopy(src, srcPos, dest, destPos, length);
			 */
			ByteBuffer bf = ByteBuffer.allocate(1024);
			
			
			
			//String params = "message=abccba&1=3";
			URLConnection conn = url.openConnection();
			HttpURLConnection httpConn = (HttpURLConnection)conn;
			httpConn.setDoOutput(false);
			httpConn.setDoInput(true);
			httpConn.setRequestMethod("GET");
			
			BufferedInputStream bis = new BufferedInputStream(httpConn.getInputStream());
			
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			for (int len=0; (len=bis.read(b))>0; ) {
				byteArrayOutputStream.write(b, 0, len);
			}
			
			bis.close();
			byteArrayOutputStream.close();
			b = byteArrayOutputStream.toByteArray();
			
			FileOutputStream fos = new FileOutputStream("/home/ddh/array.jpg");
			fos.write(b);
			fos.close();
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

}
